package edu.ccut.titan.tag.report.chart.dataset;

import edu.ccut.titan.tag.report.chart.IType;
import edu.ccut.titan.tag.report.chart.Type;
import edu.ccut.titan.tag.report.design.ChartsInfo;
import edu.ccut.titan.tag.report.design.SeriesInfo;
import net.sf.jasperreports.charts.design.JRDesignCategoryDataset;
import net.sf.jasperreports.charts.design.JRDesignCategorySeries;
import net.sf.jasperreports.engine.JRChartDataset;
import net.sf.jasperreports.engine.design.JRDesignExpression;

public class TitanCategoryDataset implements ITitanDataset {

	public JRChartDataset getDatasetContent(ChartsInfo chart) {
		JRDesignCategoryDataset categoryDataset = new JRDesignCategoryDataset(
				null);
		for (SeriesInfo series : chart.getSeries()) {
			JRDesignCategorySeries designCategorySeries = new JRDesignCategorySeries();

			JRDesignExpression categoryExpression = new JRDesignExpression();
			if (IType.FIELD_TYPE_FIELD.equals(series.getCategorytype())
					|| series.getCategorytype() == null) {
				categoryExpression.addFieldChunk(series.getCategory());
			} else if (IType.FIELD_TYPE_PARAMETER.equals(series.getCategorytype())) {
				categoryExpression.addParameterChunk(series.getCategory());
			} else if (IType.FIELD_TYPE_RESOURCE.equals(series.getCategorytype())) {
				categoryExpression.addResourceChunk(series.getCategory());
			} else if (IType.FIELD_TYPE_VARIABLE.equals(series.getCategorytype())) {
				categoryExpression.addVariableChunk(series.getCategory());
			}
			categoryExpression.setValueClassName("java.lang.Object");
			designCategorySeries.setCategoryExpression(categoryExpression);

			JRDesignExpression seriesExpression = new JRDesignExpression();
			if (IType.FIELD_TYPE_FIELD.equals(series.getSeriestype())
					|| series.getSeriestype() == null) {
				seriesExpression.addFieldChunk(series.getSeries());
			} else if (IType.FIELD_TYPE_PARAMETER
					.equals(series.getSeriestype())) {
				seriesExpression.addParameterChunk(series.getSeries());
			} else if (IType.FIELD_TYPE_RESOURCE.equals(series.getSeriestype())) {
				seriesExpression.addResourceChunk(series.getSeries());
			} else if (IType.FIELD_TYPE_VARIABLE.equals(series.getSeriestype())) {
				seriesExpression.addVariableChunk(series.getSeries());
			}
			seriesExpression.setValueClassName("java.lang.Object");
			designCategorySeries.setSeriesExpression(seriesExpression);

			JRDesignExpression valueExpression = new JRDesignExpression();
			if (IType.FIELD_TYPE_FIELD.equals(series.getValuetype())
					|| series.getValuetype() == null) {
				valueExpression.addFieldChunk(series.getValue());
			} else if (IType.FIELD_TYPE_PARAMETER.equals(series.getValuetype())) {
				valueExpression.addParameterChunk(series.getValue());
			} else if (IType.FIELD_TYPE_RESOURCE.equals(series.getValuetype())) {
				valueExpression.addResourceChunk(series.getValue());
			} else if (IType.FIELD_TYPE_VARIABLE.equals(series.getValuetype())) {
				valueExpression.addVariableChunk(series.getValue());
			}
			valueExpression.setValueClassName("java.lang.Object");
			designCategorySeries.setValueExpression(valueExpression);

			categoryDataset.addCategorySeries(designCategorySeries);
		}
		return categoryDataset;
	}
}
